/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "rtc.h"
#include "usart.h"
#include "gpio.h"
#include "fsmc.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "sram.h"
#include "define.h"
#include "WM.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
u8 test_str[] = { "LCD Test"};
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define TOUCH_TEST
#define EMWIN_TEST
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim6;
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
#ifdef SRAM_TEST
u32 i = 0;
u8 temp = 0;
u8 sval = 0; //在地址0读到的数据
#endif
#ifdef MALLOC_TEST
u8 *p1 = 0, *p2 = 0, *p3 = 0, *p4 = 0;
#endif
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_FSMC_Init();
  MX_RTC_Init();
  MX_UART5_Init();
  /* USER CODE BEGIN 2 */
  HAL_Delay(100);
  TFTLCD_Init(); //TFTLCD初始化
  TP_Init();  //触摸屏初始化

  MX_TIM3_Init();
  MX_TIM6_Init();

  my_mem_init(SRAMIN);   // 初始化内存池内部ram
  my_mem_init(SRAMEX);   //初始化内存池sram
  POINT_COLOR = BLUE;
  LCD_ShowString(0, 10, 239, 26, 16, test_str);
  LCD_ShowString(0, 40, 239, 26, 24, test_str);

#ifdef MALLOC_TEST
  p1 = mymalloc(SRAMIN, 2048);
  p2 = mymalloc(SRAMIN, 2048);
  p3 = mymalloc(SRAMIN, 2048);
  p4 = mymalloc(SRAMIN, 2048);
  if (p1 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p1);
    myfree(SRAMIN, p1);
    p1 = 0;
  }
  if (p2 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p2);
    myfree(SRAMIN, p2);
    p2 = 0;
  }
  if (p3 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p3);
    myfree(SRAMIN, p3);
    p3 = 0;
  }
  if (p4 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p4);
    myfree(SRAMIN, p4);
    p4 = 0;
  }

  p1 = mymalloc(SRAMEX, 2048);
  p2 = mymalloc(SRAMEX, 2048);
  p3 = mymalloc(SRAMEX, 2048);
  p4 = mymalloc(SRAMEX, 2048);

  if (p1 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p1);
    myfree(SRAMEX, p1);
    p1 = 0;
  }
  if (p2 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p2);
    myfree(SRAMEX, p2);
    p2 = 0;
  }
  if (p3 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p3);
    myfree(SRAMEX, p3);
    p3 = 0;
  }
  if (p4 != NULL)
  {
    printf("malloc ok,P=%x\r\n", (u32)p4);
    myfree(SRAMEX, p4);
    p4 = 0;
  }
#endif
#ifdef SRAM_TEST
  temp = 0xff;
  for (i = 0; i < 1024 * 1024; i += 4096)
  {
    FSMC_SRAM_WriteBuffer(&temp, i, 1);
    temp--;
  }
  i = 0;
#endif
  if (HAL_TIM_Base_Start_IT(&htim3) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }
  if (HAL_TIM_Base_Start_IT(&htim6) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }
  __HAL_RCC_CRC_CLK_ENABLE();
	
	
#ifdef EMWIN_TEST
  GUI_Init();	
  WM_SetCreateFlags(WM_CF_MEMDEV);
	
  GUI_SetFont(&GUI_Font8x16);
  GUI_SetBkColor(GUI_BLUE);
  GUI_Clear(); GUI_SetPenSize(10);
  GUI_SetColor(GUI_RED);
  GUI_DrawLine(80, 10, 240, 90);
  GUI_DrawLine(80, 90, 240, 10);
  GUI_SetBkColor(GUI_BLACK);
  GUI_SetColor(GUI_WHITE);
  GUI_SetTextMode(GUI_TM_NORMAL);
  GUI_DispStringHCenterAt("GUI_TM_NORMAL" , 160, 10);
	GUI_DispDecAt(lcddev.id,50,100,6);
	
#endif
	HAL_Delay(3000);
	MainTask();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    // HAL_Delay(100);
    // HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
#ifdef SRAM_TEST
    if (i < 1024 * 1024)
    {
      i += 4096;
      FSMC_SRAM_ReadBuffer(&temp, i, 1);
      printf("temp=%d\r\n", temp);
    }
#endif
#ifdef TOUCH_TEST
    tp_dev.scan(0);
    if (tp_dev.sta & TP_PRES_DOWN) //触摸屏被按下
    {
      if (tp_dev.x[0] < lcddev.width && tp_dev.y[0] < lcddev.height)
      {
        if (tp_dev.x[0] > (lcddev.width - 24) && tp_dev.y[0] < 16)LCD_Clear(WHITE); //清屏;
        else TP_Draw_Big_Point(tp_dev.x[0], tp_dev.y[0], RED); // 画图
      }
    } else delay_ms(10); //没有按键按下的时候
#endif
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
                                | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
